﻿using D010_ReplaceMethod_Add;
using Eto.Forms;
using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
using Grasshopper;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace D010_ReplaceMethod_Wire
{
    public class ReplacerPriority : GH_AssemblyPriority
    {
        public override GH_LoadingInstruction PriorityLoad()
        {
            Instances.CanvasCreated += Instances_CanvasCreated;
            return GH_LoadingInstruction.Proceed;
        }

        private void Instances_CanvasCreated(GH_Canvas canvas)
        {
            Instances.CanvasCreated -= Instances_CanvasCreated;
            GH_DocumentEditor documentEditor = Instances.DocumentEditor;
            if (documentEditor == null)
            {
                Instances.ActiveCanvas.DocumentChanged += ActiveCanvas_DocumentChanged;
            }
            else
            {
                DoingSomethingFirst();
            }
        }

        private void ActiveCanvas_DocumentChanged(GH_Canvas sender, GH_CanvasDocumentChangedEventArgs e)
        {
            Instances.ActiveCanvas.DocumentChanged -= ActiveCanvas_DocumentChanged;
            GH_DocumentEditor documentEditor = Instances.DocumentEditor;
            if (documentEditor == null)
            {
                MessageBox.Show("没找到文档");
            }
            else
            {
                DoingSomethingFirst();
            }
        }

        private void DoingSomethingFirst()
        {
            MenuCreater.CreateMenus(); // 添加菜单
            Replacer.Init(); // 替换方法
        }
    }
}
